package cn.breeze.org.controller;

import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.PageList;
import cn.breeze.org.domain.Employee;
import cn.breeze.org.query.EmployeeQuery;
import cn.breeze.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /***
    * @Title: loadById
    * @Description: 通过ID查询
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/6/24 11:51
    * @Parameters: [id]
    * @Return cn.breeze.org.domain.Employee
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee loadById(@ApiParam(name="ID属性",required = true) @PathVariable Long id){
        return employeeService.loadById(id);
    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/6/24 11:51
    * @Parameters: []
    * @Return java.util.List<cn.breeze.org.domain.Employee>
    */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 根据id删除
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/6/24 11:51
    * @Parameters: [id]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @ApiOperation(value = "根据id删除" )
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试试！");
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增或者修改
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/6/24 11:52
    * @Parameters: [employee]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PutMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        System.out.println(employee);
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试试！");
        }
    }

    /**
    * @Title: pageQuery
    * @Description: 高级和分页查询
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/6/24 11:54
    * @Parameters: [employeeQuery]
    * @Return cn.breeze.basic.utils.PageList<cn.breeze.org.domain.Employee>
    */
    @ApiOperation(value = "高级和分页查询")
    @PostMapping
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryData(employeeQuery);
    }
}
